//
//  KLMainTabBarController.swift
//  BCWeiBo
//
//  Created by bcmac3 on 16/3/30.
//  Copyright © 2016年 KellenYangs. All rights reserved.
//

import UIKit

class KLMainTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.whiteColor()
        
        // 添加子控制器
        addChildViewControllers()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        // 初始化加号按钮
        setupComposeBtn()
    }
    
    /**
     添加所有子控制器
     */
    private func addChildViewControllers() {
        // 1. 获取json文件路径
        let jsonPath = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil)
        // 2. 加载json数据
        let jsonData = NSData(contentsOfFile: jsonPath!)
        // 3. 序列化json
        do{
            let dictArray = try NSJSONSerialization.JSONObjectWithData(jsonData!, options: .MutableContainers)
            // 便利字典时需要指明数组中的数据类型
            for dict in dictArray as! [[String : String]]
            {
                addChildViewController(dict["vcName"]!, title: dict["title"]!, imageName: dict["imageName"]!)
            }
        }catch{
            addChildViewController("KLHomeTableViewController", title: "首页", imageName: "tabbar_home")
            addChildViewController("KLMessageTableViewController", title: "消息", imageName: "tabbar_message_center")
            // 添加占位控制器
            addChildViewController("KLNullViewController", title: "", imageName: "")
            addChildViewController("KLDiscoverTableViewController", title: "发现", imageName: "tabbar_discover")
            addChildViewController("KLProfileTableViewController", title: "我", imageName: "tabbar_profile")
        }
    }
    
    /**
     初始化子控制器
     
     - parameter childControllerName: 需要初始化的子控制器名
     - parameter title:               初始化的标题
     - parameter imageName:           初始化的图片
     */
    private func addChildViewController(childControllerName: String, title: String, imageName: String) {
        // 0. 动态获取命名空间
        let namespace = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
        
        // 1. 将字符串转换成类，通过类创建对象
        let cls:AnyClass = NSClassFromString(namespace + "." + childControllerName)!
        
        let vcCls = cls as! UIViewController.Type
        let vc = vcCls.init()
        
        // 从内像外设置, nav和tabbar都有
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
        
        // 注意: Xocde7之前只有文字有效果, 还需要设置图片渲染模式
        tabBar.tintColor = UIColor.orangeColor()
        
        // 2.创建导航控制器
        let nav = UINavigationController()
        nav.addChildViewController(vc)
        
        // 3.添加控制器到tabbarVC
        addChildViewController(nav)
    }
    
    // MARK: - 内部控制方法
    
    /**
    设置加号按钮位置
    */
    private func setupComposeBtn(){
        
        // 0.添加到tabBar上
        tabBar.addSubview(composeBtn)
        
        // 1.计算按钮宽度
        let width = tabBar.bounds.width / CGFloat(viewControllers!.count)
        // 2.创建按钮frame
        let rect = CGRect(x: 0, y: 0, width: width, height: tabBar.bounds.height)
        // 3.设置按钮frame和偏移位
        composeBtn.frame = CGRectOffset(rect, width * 2, 0)
    }
    
    /// 加号按钮
    private lazy var composeBtn : UIButton = {
        // 1.创建按钮
        let button = UIButton()
        // 2.设置图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        // 3.设置背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        // 4.监听加号按钮点击
        button.addTarget(self, action: "composeBtnClick", forControlEvents: .TouchUpInside)
        
        // 5.返回按钮
        return button
    }()
    
    /**
     监听加号按钮点击
     */
    func composeBtnClick(){
        let nav = UINavigationController(rootViewController: KLComposeViewController())
        
        presentViewController(nav, animated: true, completion: nil)
    }
    
}
